package com.pega.eprescription.activity;

import java.util.ArrayList;
import java.util.List;

import com.pega.eprescription.R;
import com.pega.eprescription.config.Data;
import com.pega.eprescription.customview.MySeekBar;
import com.pega.eprescription.database.manager.DatabaseManager;
import com.pega.eprescription.database.model.Pill;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;

public class EditDrug extends Activity {
	
	private Pill mPill;
	private int mIdReciver;
	private NumberPicker npMorning = null;
	private NumberPicker npNoon = null;
	private NumberPicker npEverning = null;
	private MySeekBar mseekBar = null;
	private AutoCompleteTextView tvNamePill = null;
	private int mIdParent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_drug);		
		
		mIdReciver =getIntent().getIntExtra("ID_PILL_PASS", -100);
		mIdParent= getIntent().getIntExtra("ID_PRESCRIPT_PASS", -111);
		if (mIdReciver != -100) {
			mPill = DatabaseManager.getInstance(getApplicationContext()).getPillRecordWithId(mIdReciver);
		}
		else {
		      mPill = new Pill();
		      if (mIdParent!= -111) 
		    	  mPill.setPreID(mIdParent);
		}
		
		
		tvNamePill = (AutoCompleteTextView) findViewById(R.id.et_name_of_pill);		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Data.NAME_OF_PILL);
		tvNamePill.setAdapter(adapter);
		if (mIdReciver != -100) {
			tvNamePill.setText(mPill.getNamePill());
		}
		
		
		
		
		npMorning = (NumberPicker) findViewById(R.id.npMorning);
		npMorning.setMaxValue(20);
		npMorning.setMinValue(0);
		npMorning.setWrapSelectorWheel(false);
		if (mIdReciver != -100) {
			npMorning.setValue(mPill.getAmountMorning());
		}
		else npMorning.setValue(1);
		
		npNoon = (NumberPicker) findViewById(R.id.npNoon);
		npNoon.setMaxValue(20);
		npNoon.setMinValue(0);
		npNoon.setWrapSelectorWheel(false);
		if (mIdReciver != -100) {
			npNoon.setValue(mPill.getAmountNoon());
		}
		else npNoon.setValue(1);
		
		npEverning = (NumberPicker) findViewById(R.id.npEverning);
		npEverning.setMaxValue(20);
		npEverning.setMinValue(0);
		npEverning.setWrapSelectorWheel(false);	
		if (mIdReciver != -100) {
			npEverning.setValue(mPill.getAmountEverning());
		}
		else npEverning.setValue(1);
		
		
		Button btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		Button btnFinish = (Button) findViewById(R.id.btnFinish);
		btnFinish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				//check if prescription recive from inttent not null
				//update infromation
				//else add new			
				getPillInforChange();
				if (mIdReciver != -100) {
					DatabaseManager.getInstance(getApplicationContext()).updatePillRecord(mPill);
				}
				else 
					DatabaseManager.getInstance(getApplicationContext()).addPillRecord(mPill);
					
				finish();
			}
		});
		
		
		mseekBar = (MySeekBar) this
				.findViewById(R.id.ans_seekbar);

		mseekBar.setIsThumbTitle(true);
		List<Integer> stopSeek = new ArrayList<Integer>();
		int min = 1;
		int max = 150;
		int step = 1;


		for (int i = min; i <= max; i = i + (step)) 
		{
			stopSeek.add(i);
		}
		mseekBar.setStopSeek(stopSeek);
		mseekBar.setMinTitle(String.valueOf(min));
		mseekBar.setMaxTitle(String.valueOf(max));
		mseekBar.setOnTouchListener(new OnTouchListener()
        {
			@Override
			public boolean onTouch( View v,  MotionEvent event)
            {
				mseekBar.onTouchListener.onTouch(v, event);
				int[] position = new int[2];
				v.getLocationInWindow(position);
				int progress = (int) ((event.getRawX() - position[0])
						* mseekBar.getMax() / mseekBar.getWidth());

				if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
					return true;
				}
				return false;
			}
		});
	}
	
	private void getPillInforChange() {
		mPill.setNamePill(tvNamePill.getText().toString());
		mPill.setAmountMorning(npMorning.getValue());
		mPill.setAmountNoon(npNoon.getValue());
		mPill.setAmountEverning(npEverning.getValue());
		mPill.setAmount(mseekBar.getCurrentValue());
	}
	
}
